﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace my_hw5_director
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TreeNode my_computer_node = new TreeNode("我的电脑");
            treeView1.Nodes.Add(my_computer_node);
            ListViewShow(my_computer_node);
        }
        private void ListViewShow(TreeNode node)
        {
            listView1.Clear();
            if(node.Parent == null)
            {
                foreach (string item in Directory.GetLogicalDrives())
                {
                    listView1.Items.Add(item);
                }
            }
            else
            {
                foreach (string item in Directory.GetDirectories((string)node.Text))
                {
                    listView1.Items.Add(item);
                }
            }
        }
        private void TreeViewShow(TreeNode node)
        {
            if(node.Nodes.Count == 0)
            {
                if (node.Parent == null)
                {
                    foreach (string item in Directory.GetLogicalDrives())
                    {
                        node.Nodes.Add(item);
                    }
                }
                else
                {
                    foreach (string item in Directory.GetFileSystemEntries((string)node.Text))
                    {
                        node.Nodes.Add(item);
                    }
                }
            }
        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ListViewShow(e.Node);
            TreeViewShow(e.Node);
        }
    }
}
